<template>
  <div class="dialog">
    <el-dialog :title="dialog.title" v-model="dialog.show">
      <el-form :model="formData" ref="form" :rules="form_rules">
        <el-form-item label="收支类型" prop="type" label-width="140px">
          <el-select v-model="formData.type" placeholder="请选择收支类型">
            <el-option
              v-for="(formType, index) in form_type_list"
              :key="index"
              :value="formType"
              :label="formType"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收支描述" prop="description" label-width="140px">
          <el-input
            v-model="formData.description"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="收入" prop="income" label-width="140px">
          <el-input v-model="formData.income" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="支出" prop="expenditure" label-width="140px">
          <el-input
            v-model="formData.expenditure"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="账户现金" prop="account" label-width="140px">
          <el-input v-model="formData.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark" label-width="140px">
          <el-input v-model="formData.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <!-- 确认取消按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.show = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit(form)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  getCurrentInstance,
} from "vue";
const { proxy } = getCurrentInstance();
const emits = defineEmits(["sendMsg"]);

const form = ref(null);
const props = defineProps({
  dialog: Object,
  formData: Object,
});
const form_type_list = ref([
  "提现",
  "提现手续费",
  "充值",
  "优惠券",
  "充值礼券",
  "转账",
]);
const form_rules = reactive({
  type: [{ required: true, message: "收支类型不能为空", trigger: "blur" }],
  description: [
    { required: true, message: "收支表述不能为空", trigger: "blur" },
  ],
  income: [{ required: true, message: "收入不能为空", trigger: "blur" }],
  expenditure: [{ required: true, message: "支出不能为空", trigger: "blur" }],
  account: [{ required: true, message: "账户现金不能为空", trigger: "blur" }],
});
const onSubmit = (form) => {
  form
    .validate((valid) => {
      console.log(form);
      if (valid) {
        const url =
          props.dialog.option == "add" ? "add" : `edit/${props.formData.id}`;
        proxy.$axios.post(`/funds/${url}`, props.formData).then((res) => {
          if (props.dialog.option == "edit") {
            proxy.$message({ message: "数据更新成功", type: "success" });
          } else {
            proxy.$message({ message: "数据添加成功", type: "success" });
          }
          props.dialog.show = false;
          emits("update");
        });
      }
    })
    .catch((err) => {
      proxy.$message({ message: "请求失败，请重试", type: "error" });
      console.log(err);
    });
};
</script>

<style lang="less" scoped>
</style>
